package com.csesteel.crm.dao.impl;

import com.csesteel.common.dao.impl.BaseDaoHibernate4;
import com.csesteel.crm.dao.UserDao;
import com.csesteel.crm.domain.User;
import org.hibernate.Query;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2015/4/21 0021.
 */
@Resource
public class UserDaoImpl extends BaseDaoHibernate4<User>
        implements UserDao
{
        public User getUserByNameAndPassword(User user) {
                List<User> users = find("select u from User u where u.username = ?0 and u.password = ?1"
                        , user.getUsername(),user.getPassword());
                if (users!= null && users.size() >= 1)
                {
                        return users.get(0);
                }
                return null;
        }

        public void authUserByRole(Integer userId, Integer roleId) {
                String sql ="insert into crm_r_user_role (user_role_id,user_id,role_id) values (SEQ_CRM_USER_ROLE.Nextval,"+userId+","+roleId+")";
                saveWithSql(sql);
        }

        public User getUserByNameAndPassword(String name, String passwd) {
                List<User> users = find("select u from User u where u.username = ?0 and u.password = ?1"
                        , name,passwd);
                if (users!= null && users.size() >= 1)
                {
                        return users.get(0);
                }
                return null;
        }


        public void deleteUserById(String id) {
                String deletesql = "update crm_tb_user set delstatus =?0 where user_id = ?1";
                operateSQLWithParams(deletesql,1,id);
//                String deletesql = "update crm_tb_user set delstatus =1 where user_id = "+id;
//                deleteBySQL(deletesql);
        }

        public User getByUsername(String username) {
                List<User> users = find("select u from User u where u.username = ?0"
                        , username);
                if (users!= null && users.size() >= 1)
                {
                        return users.get(0);
                }
                return null;
        }

        public Collection<GrantedAuthority> loadUserAuthorities(String username) {
                List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
                authList.add(new SimpleGrantedAuthority("ROLE_USER"));
                return authList;
        }

        public List<User> findByPage( int pageNo, int pageSize
                , String name)
        {
                String hql = null;
                if((name == null) ||("".equals(name)))
                {
                        hql = "select u from User u where u.delstatus =0 order by userId desc";
                }else
                {
                        hql="select u from User u where u.delstatus =0 and u.username like '%"+name+"%'order by userId desc";
                }

                return findByPage(hql,pageNo,pageSize);
        }

        public List<User> findByPageNoparams(int pageNo, int pageSize) {
                String hql = "select u from User u where u.delstatus =0 order by userId desc";
                return findByPage(hql, pageNo, pageSize);
        }

        public long findCountUsersWithParams(String name) {
                List<?> l = find("select count(*) from User u where u.delstatus =0 and u.username like '%"+name+"%'");
                // 返回查询得到的实体总数
                if (l != null && l.size() == 1 )
                {
                        return (Long)l.get(0);
                }
                return 0;
        }

        public long findCountUsersNohParams() {
                List<?> l = find("select count(*) from User u where u.delstatus =0");
                // 返回查询得到的实体总数
                if (l != null && l.size() == 1 )
                {
                        return (Long)l.get(0);
                }
                return 0;
        }
}
